<!-- file users -->
<script> 
	
	$(function(){
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
			if (this.id && this.id !== '') {
				if (ids[this.id]) {
					$(this).remove();
				} else {
					ids[this.id] = this
				}
			}
		});
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_users = $("#task_users");
		grid_users = $("#grid-users");
		pgrid_users = $("#pgrid-users");
		btnadd_users = $("#btn-add-users");
		btnedit_users = $("#btn-edit-users");
		btndelete_users = $("#btn-delete-users");
		btncancel_users =  $("#btn-cancel-users");
		btnsave_users = $("#btn-save-users");
		form_users = $("#form-users");
		
		field_user_id = $("#user_id");
		field_user_name = $("#user_name");
		field_user_passwd = $("#user_passwd");
		field_user_repeat = $("#user_repeat");
		user_group = $("#user_group");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_users.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_users',
			datatype: "json", 
			colNames:[
				'Username','Group','','user_id'],
			colModel:[
				{ name: 'user_name'  ,index: 'user_name' , search : true},
				{ name: 'group_name'  ,index: 'group_name' , search : true},
				{ name: 'user_groups'  ,index: 'user_groups' ,hidden: true,  search : true},
				{ name: 'user_id'  ,index: 'user_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-users', 
			sortname: 'user_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:""
		}); 
		grid_users.jqGrid('navGrid','#pgrid-users',{edit:false,add:false,del:false,search:false});
		grid_users.jqGrid('filterToolbar',{});
		
		form_users.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */
		
		btnedit_users.click(function(){
			id = grid_users.jqGrid("getGridParam","selrow");
			if (id != null){
				task_users.val("UPDATE");
				var data = $(this).data();
				dataRow = grid_users.getRowData(id);
				field_user_id.val(dataRow.user_id);
				field_user_name.val(dataRow.user_name);
				user_group.val(dataRow.user_groups);
				form_users.dialog("open");
			}
		});
		
		btndelete_users.click(function(){
			id = grid_users.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					var data = $(this).data();
					$.ajax({
						type: 'POST',
						url: 'c_users',
						data: {user_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_users.trigger('reloadGrid');
								$("#grid-users").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		
		btnadd_users.click(function(){
			task_users.val("INSERT");
			form_users.dialog("open");
		});
		
		btncancel_users.click(function(){
			form_users.dialog("close");
		});
		
		btnsave_users.click(function(){
			if (field_user_repeat.val() !=  field_user_passwd.val()){
				alert("Password tidak cocok!");
			}else{
				data = form_users.serializeObject();
				$.ajax({
					url : "c_users",
					data : data,
					type : 'POST'
				}).done(function(response){
					grid_users.trigger('reloadGrid');
					btncancel_users.click();
				});
			}
		});
		
	});
</script>

	<table id="grid-users" class="table table-striped table-bordered" ></table>
	<div id="pgrid-users"></div>
	
	<button id="btn-add-users">Tambah</button>
	<button id="btn-edit-users">Edit</button>
	<button id="btn-delete-users">Delete</button>
	
	<form id="form-users" title="users" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_users">
	   <table>
			<tr style="display: none">
				<td align="right">user_id:</td>
				<td align="left"><input id="user_id" name="user_id" /></td>
			</tr>
			
			<tr > <td align="right">Username:</td> <td align="left"><input id="user_name" name="user_name" /></td> </tr>
			<tr > <td align="right">Group:</td> <td align="left"><?php echo form_dropdown("user_group",$s_usergroups,"","id=user_group"); ?></td> </tr>
			<tr > <td align="right">password:</td> <td align="left"><input id="user_passwd" type="password" name="user_passwd" /></td> </tr>
			<tr > <td align="right">repeat:</td> <td align="left"><input id="user_repeat" type="password" name="user_repeat" /></td> </tr>
			
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-users" value="Save"  />
					<input id="btn-cancel-users" type="reset" value="Cancel" /></td>
			</tr>
		</table>
	</form>